package com.dudu.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.ParamBean;
import com.dudu.bean.PoolAbilityInfoBean;
import com.dudu.bean.PoolNumberBean;
import com.dudu.bean.UserBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.service.ILogService;
import com.dudu.service.IPoolNumberService;
import com.dudu.service.impl.LogService;
import com.dudu.service.impl.PoolNumberServiceImpl;
import com.dudu.utils.EnumUtility;

import core.CoreService;
/**
 * 号码池管理(管理后台) 
 * @author gaoxs
 *
 */
public class PoolNumberController extends CoreService implements IPoolNumberController {
	//当前登陆用户
	private  final String KEY_CURRENT_USER = "key_current_user";
	//用于记录相应接口调用的日志
	private ILogService logService = new LogService();
	private IPoolNumberService poolNumberService=new PoolNumberServiceImpl();
	
	/**
     * 查询所有省份的名称
     * @return
     */
    public String queryAllAreas(){
    	log.info("PoolNumberController.queryAllAreas");
		//放在JSONObject里面的数据对象
        JSONArray jsonArray = new JSONArray();
        List<ParamBean> dbnames = null;
		//获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取前台传入的数据
			dbnames = poolNumberService.queryAllAreas();
			//将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(dbnames);
		}
		return returnData(jsonArray.toString());
    }
    /**
	 * 查询所有NumberPool信息
	 * @return
	 */
	public String queryAllNumber() {
		log.info("PoolNumberController.queryAllNumber");
		// 获取查询得到的list
		List<PoolNumberBean> numberPoolList=null;
		//最后返回的对象
		JSONObject object = new JSONObject();
		//放在JSONObject里面的数据对象
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        //查询数据
	        numberPoolList=poolNumberService.queryAll(pageSql,paramSql);
			//查询总数量
			int totalAcount=poolNumberService.getTotalNumber( paramSql);
			//将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(numberPoolList);
			JSONObject dataobject = new JSONObject();
			//总数量信息
			dataobject.put("totalItemNum", totalAcount);
			//具体数据信息
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
			object.put("flag", "000000");
	        object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
     * 插入NumberPool信息
     * @return
     */
	public String doInsert() {
		log.info("PoolNumberController.doInsert");
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			// 转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			// 获取相应数据
			String phone_number = data.getString("phone_number");// 手机号码
			String number_state = data.getString("number_state");// 号码状态
			String number_vest_name = data.getString("number_vest_name");// 号码归属地名称
			String number_vest = data.getString("number_vest");// 号码归属地
			String beautiful_type = data.getString("beautiful_type");// 靓号类型
			String number_source = data.getString("number_source");// 号码来源
			String number_comment = data.getString("number_comment");// 备注信息
			String is_resue = data.getString("is_resue");// 是否可重用
			//创建时间的格式化
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
	        String create_time = sdf.format(cal.getTime());// 创建时间
	        String create_user = currentUser.getName();// 数据创建人

			if (StringUtils.isNotBlank(phone_number)) {//手机号码
				PoolNumberBean tmpNumberPool = new PoolNumberBean();
				tmpNumberPool.setPhone_number(phone_number);//手机号码
				tmpNumberPool.setNumber_state(number_state);// 号码状态
				tmpNumberPool.setNumber_vest_name(number_vest_name);// 号码归属地名称
				tmpNumberPool.setNumber_vest(number_vest);// 号码归属地
				tmpNumberPool.setBeautiful_type(beautiful_type);// 靓号类型
				tmpNumberPool.setNumber_source(number_source);// 号码来源
				tmpNumberPool.setNumber_comment(number_comment);// 备注信息
				tmpNumberPool.setIs_resue(is_resue);// 是否可重用
				tmpNumberPool.setCreate_time(create_time);// 创建时间
				tmpNumberPool.setUpdate_time(create_time);// 更新时间
				tmpNumberPool.setCreate_user(create_user);// 数据创建人
				tmpNumberPool.setUpdate_user(create_user);// 数据更新人

				// 执行数据插入
				int numberPoolIn = this.poolNumberService.doInsert(tmpNumberPool);
				// 日志输出信息
				logService.log(OperateTypeEnum.ADD.getKey(), currentUser.getId(), currentUser.getName(), 
						"t_pool_number", "手机号码：" + tmpNumberPool.getPhone_number() + 
						"|号码状态：" + tmpNumberPool.getNumber_state() +
						"|号码归属地名称："+tmpNumberPool.getNumber_vest_name() +
						"|号码归属地："+tmpNumberPool.getNumber_vest() +
						"|靓号类型："+tmpNumberPool.getBeautiful_type() +
						"|号码来源："+tmpNumberPool.getNumber_source() +
						"|备注信息："+tmpNumberPool.getNumber_comment() +
						"|是否可重用："+tmpNumberPool.getIs_resue() +
						"|创建时间："+tmpNumberPool.getCreate_time() +
						"|修改时间："+tmpNumberPool.getUpdate_time() +
						"|数据创建人："+tmpNumberPool.getCreate_user() +
						"|数据修改人："+tmpNumberPool.getUpdate_user());

				if (numberPoolIn != -1) {
					object.put("flag", "000000");
					object.put("desc", "success");
				} else {
					object.put("flag", "100000");
				}
			}
		} else {
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}

	/**
     * 编辑NumberPool信息
     * @return
     */
	public String doEdit() {
		log.info("PoolNumberController.doEdit");
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			// 转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			// 获取相应数据
			String id = data.getString("id");// id
			String phone_number = data.getString("phone_number");// 手机号码
			String number_state = data.getString("number_state");// 号码状态
			String number_vest_name = data.getString("number_vest_name");// 号码归属地名称
			String number_vest = data.getString("number_vest");// 号码归属地
			String beautiful_type = data.getString("beautiful_type");// 靓号类型
			String number_source = data.getString("number_source");// 号码来源
			String number_comment = data.getString("number_comment");// 备注信息
			String is_resue = data.getString("is_resue");// 是否可重用
	        //创建时间的格式化
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
	        String create_time = sdf.format(cal.getTime());// 修改时间
	        String create_user = currentUser.getName();// 数据修改人
	        
			// 实体类
	        PoolNumberBean tmpNumberPool = new PoolNumberBean();
			if (!StringUtils.isEmpty(id)) {
				tmpNumberPool.setId(Integer.parseInt(id));//id
			}
			// 插入数据
			tmpNumberPool.setPhone_number(phone_number);// 手机号码
			tmpNumberPool.setNumber_state(number_state);// 号码状态
			tmpNumberPool.setNumber_vest_name(number_vest_name);// 号码归属地名称
			tmpNumberPool.setNumber_vest(number_vest);// 号码归属地
			tmpNumberPool.setBeautiful_type(beautiful_type);// 靓号类型
			tmpNumberPool.setNumber_source(number_source);// 号码来源
			tmpNumberPool.setNumber_comment(number_comment);// 备注信息
			tmpNumberPool.setIs_resue(is_resue);// 是否可重用
			tmpNumberPool.setUpdate_time(create_time);// 修改时间
			tmpNumberPool.setUpdate_user(create_user);// 数据修改人
			// 执行数据插入
			int numberPoolIn = this.poolNumberService.doEdit(tmpNumberPool);
			// 日志输出信息
			logService.log(OperateTypeEnum.EDIT.getKey(), currentUser.getId(), currentUser.getName(), 
					"t_pool_number", "手机号码：" + tmpNumberPool.getPhone_number() + 
					"|号码状态：" + tmpNumberPool.getNumber_state() +
					"|号码归属地名称："+tmpNumberPool.getNumber_vest_name() +
					"|号码归属地："+tmpNumberPool.getNumber_vest() +
					"|靓号类型："+tmpNumberPool.getBeautiful_type() +
					"|号码来源："+tmpNumberPool.getNumber_source() +
					"|备注信息："+tmpNumberPool.getNumber_comment() +
					"|是否可重用："+tmpNumberPool.getIs_resue() +
					"|创建时间："+tmpNumberPool.getCreate_time() +
					"|修改时间："+tmpNumberPool.getUpdate_time() +
					"|数据创建人："+tmpNumberPool.getCreate_user() +
					"|数据修改人："+tmpNumberPool.getUpdate_user());
			if(numberPoolIn!=-1){
	        	object.put("flag", "000000");
		        object.put("desc", "success");
	        }else{
	        	object.put("flag", "100000");
	        }
		} else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	
	
	/**
	 * 根据能力类型，查询所有已配置能力的号码
	 * @return
	 */
	
	public String queryNumbersByAbility() {
		log.info("PoolNumberController.queryNumbersByAbility");
		// 获取查询得到的list
		List<PoolNumberBean> numberPoolList=null;
		//最后返回的对象
		JSONObject object = new JSONObject();
		//放在JSONObject里面的数据对象
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取能力类型
			String abilityType = (String) this.getRequest().getParameter("abilityType");
			// 获取账号id
			String accountId = (String) this.getRequest().getParameter("accountId");
			//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        //查询数据
	        numberPoolList=this.poolNumberService.queryNumbersByAbility(pageSql,paramSql,accountId,abilityType);
			//查询总数量
			int totalAcount=poolNumberService.getTotalNumberByAbility(paramSql, accountId, abilityType);
			//将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(numberPoolList);
			
			JSONObject dataobject = new JSONObject();
			//总数量信息
			dataobject.put("totalItemNum", totalAcount);
			//具体数据信息
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
			
			object.put("flag", "000000");
	        object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	
	/**
	 * 根据能力类型，查询所有未配置能力的号码
	 * @return    queryUnConfNumByAbility
	 */
	public String queryUnConfNumByAbility() {
		log.info("PoolNumberController.queryUnConfNumByAbility");
		// 获取查询得到的list
		List<PoolNumberBean> numberPoolList=null;
		//最后返回的对象
		JSONObject object = new JSONObject();
		//放在JSONObject里面的数据对象
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取能力类型
			String abilityType = (String) this.getRequest().getParameter("abilityType");
			String app_key = (String) this.getRequest().getParameter("app_key");
			String cust_account=(String)this.getRequest().getParameter("cust_account");
			if(EnumUtility.isBlank(cust_account)){
				cust_account="-1";
			}
			//String abilityType = "";
			//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        //查询数据
	        numberPoolList=this.poolNumberService.queryUnConfNumByAbility(pageSql,paramSql,abilityType,app_key,cust_account);
			//查询总数量
			int totalAcount=poolNumberService.getTotalUnConfNum( paramSql, abilityType,app_key,cust_account);
			//将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(numberPoolList);
			
			JSONObject dataobject = new JSONObject();
			//总数量信息
			dataobject.put("totalItemNum", totalAcount);
			//具体数据信息
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
			
			object.put("flag", "000000");
	        object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 根据号码池中号码的id，获取该号码已配置的号码能力关系
	 * @return
	 */
	public String getRelByPoolId() {
		log.info("PoolNumberController.getRelByPoolId");
		// 获取查询得到的list
		List<PoolAbilityInfoBean> PoolAbilityInfoList=null;
		//最后返回的对象
		JSONObject object = new JSONObject();
		//放在JSONObject里面的数据对象
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
	        // 获取前台传入的数据
			String poolId = (String) this.getRequest().getParameter("poolId");
	        //查询数据
	        PoolAbilityInfoList = this.poolNumberService.getRelByPoolId(poolId);
			//查询总数量
	        int totalAcount = 0;
	        if(PoolAbilityInfoList != null){
	        	totalAcount = PoolAbilityInfoList.size();
	        }
			//将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(PoolAbilityInfoList);
			
			JSONObject dataobject = new JSONObject();
			//总数量信息
			dataobject.put("totalItemNum", totalAcount);
			//具体数据信息
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
			
			object.put("flag", "000000");
	        object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
     * 插入能力号码配置关系信息
     * @return
     */
	public String doInsertPoolAbility() {
		log.info("PoolNumberController.doInsert");
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			// 转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			// 获取相应数据
			String pool_id = data.getString("pool_id");// 对应号码池号码id
			String ability_type = data.getString("ability_type");// 号码开通的能力类型
			String display_number = data.getString("display_number");// 主显号码
			String charge_number = data.getString("charge_number");// 能力账号
			String charge_key = data.getString("charge_key");// 能力密码
			String reserve_column1 = data.getString("reserve_column1");// 预留字段1
			String reserve_column2 = data.getString("reserve_column2");// 预留字段2
			String comment_info = data.getString("comment_info");// 备注信息
			String limit_num = data.getString("limit_num");// 每日限制通话个数
			//创建时间的格式化
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
	        String create_time = sdf.format(cal.getTime());// 创建时间
	        String create_user = currentUser.getName();// 数据创建人

			if (StringUtils.isNotBlank(pool_id)) {
				// PoolAbilityInfo实体类
				PoolAbilityInfoBean tmpPoolAbility = new PoolAbilityInfoBean();

				tmpPoolAbility.setPool_id(pool_id);//对应号码池号码id
				tmpPoolAbility.setAbility_type(ability_type);// 号码开通的能力类型
				tmpPoolAbility.setDisplay_number(display_number);;// 主显号码
				tmpPoolAbility.setCharge_number(charge_number);// 能力账号
				tmpPoolAbility.setCharge_key(charge_key);// 能力密码
				tmpPoolAbility.setReserve_column1(reserve_column1);// 预留字段1
				tmpPoolAbility.setReserve_column2(reserve_column2);// 预留字段2
				tmpPoolAbility.setComment_info(comment_info);// 备注信息
				tmpPoolAbility.setLimit_num(limit_num);// 每日限制通话个数
				tmpPoolAbility.setCreate_time(create_time);// 创建时间
				tmpPoolAbility.setUpdate_time(create_time);
				tmpPoolAbility.setCreate_user(create_user);// 数据创建人
				tmpPoolAbility.setUpdate_user(create_user);

				// 执行数据插入
				int poolAbilityIn = this.poolNumberService.doInsertPoolAbility(tmpPoolAbility);
				// 日志输出信息
				logService.log(OperateTypeEnum.ADD.getKey(), currentUser.getId(), currentUser.getName(), 
						"t_pool_ability_info", "对应号码池号码id：" + tmpPoolAbility.getPool_id() + 
						"|号码开通的能力类型：" + tmpPoolAbility.getAbility_type() +
						"|主显号码："+tmpPoolAbility.getDisplay_number() +
						"|能力账号："+tmpPoolAbility.getCharge_number() +
						"|能力密码："+tmpPoolAbility.getCharge_key() +
						"|预留字段1："+tmpPoolAbility.getReserve_column1() +
						"|预留字段2："+tmpPoolAbility.getReserve_column2() +
						"|备注信息："+tmpPoolAbility.getComment_info() +
						"|每日限制通话个数："+tmpPoolAbility.getLimit_num() +
						"|创建时间："+tmpPoolAbility.getCreate_time() +
						"|修改时间："+tmpPoolAbility.getUpdate_time() +
						"|数据创建人："+tmpPoolAbility.getCreate_user() +
						"|数据修改人："+tmpPoolAbility.getUpdate_user());

				if (poolAbilityIn != -1) {
					object.put("flag", "000000");
					object.put("desc", "success");
				} else {
					object.put("flag", "100000");
				}
			}
		} else {
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}

	/**
     * 编辑能力号码配置关系信息
     * @return
     */
	public String doEditPoolAbility() {
		log.info("PoolNumberController.doEdit");
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			// 转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			// 获取相应数据
			String id = data.getString("id");// id
			String pool_id = data.getString("pool_id");// 对应号码池号码id
			String ability_type = data.getString("ability_type");// 号码开通的能力类型
			String display_number = data.getString("display_number");// 主显号码
			String charge_number = data.getString("charge_number");// 能力账号
			String charge_key = data.getString("charge_key");// 能力密码
			String reserve_column1 = data.getString("reserve_column1");// 预留字段1
			String reserve_column2 = data.getString("reserve_column2");// 预留字段2
			String comment_info = data.getString("comment_info");// 备注信息
			String limit_num = data.getString("limit_num");// 每日限制通话个数
	        //创建时间的格式化
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
	        String create_time = sdf.format(cal.getTime());// 修改时间
	        String create_user = currentUser.getName();// 数据修改人
	        
	        // PoolAbilityInfo实体类
 			PoolAbilityInfoBean tmpPoolAbility = new PoolAbilityInfoBean();
			if (!StringUtils.isEmpty(id)) {
				tmpPoolAbility.setId(Integer.parseInt(id));// id
			}
			// 插入数据
			

			tmpPoolAbility.setPool_id(pool_id);// 对应号码池号码id
			tmpPoolAbility.setAbility_type(ability_type);// 号码开通的能力类型
			tmpPoolAbility.setDisplay_number(display_number);// 主显号码
			tmpPoolAbility.setCharge_number(charge_number);// 能力账号
			tmpPoolAbility.setCharge_key(charge_key);// 能力密码
			tmpPoolAbility.setReserve_column1(reserve_column1);// 预留字段1
			tmpPoolAbility.setReserve_column2(reserve_column2);// 预留字段2
			tmpPoolAbility.setComment_info(comment_info);// 备注信息
			tmpPoolAbility.setLimit_num(limit_num);// 每日限制通话个数
			tmpPoolAbility.setUpdate_time(create_time);
			tmpPoolAbility.setUpdate_user(create_user);
			
			// 执行数据插入
			int numberPoolIn = this.poolNumberService.doEditPoolAbility(tmpPoolAbility);
			
			// 日志输出信息
			logService.log(OperateTypeEnum.EDIT.getKey(), currentUser.getId(), currentUser.getName(), 
					"t_pool_ability_info", "对应号码池号码id：" + tmpPoolAbility.getPool_id() + 
					"|号码开通的能力类型：" + tmpPoolAbility.getAbility_type() +
					"|主显号码："+tmpPoolAbility.getDisplay_number() +
					"|能力账号："+tmpPoolAbility.getCharge_number() +
					"|能力密码："+tmpPoolAbility.getCharge_key() +
					"|预留字段1："+tmpPoolAbility.getReserve_column1() +
					"|预留字段2："+tmpPoolAbility.getReserve_column2() +
					"|备注信息："+tmpPoolAbility.getComment_info() +
					"|每日限制通话个数："+tmpPoolAbility.getLimit_num() +
					"|创建时间："+tmpPoolAbility.getCreate_time() +
					"|修改时间："+tmpPoolAbility.getUpdate_time() +
					"|数据创建人："+tmpPoolAbility.getCreate_user() +
					"|数据修改人："+tmpPoolAbility.getUpdate_user());

			
			if(numberPoolIn!=-1){
	        	object.put("flag", "000000");
		        object.put("desc", "success");
	        }else{
	        	object.put("flag", "100000");
	        }
		} else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
}
